//
//  MovieCollectViewController.m
//  UI_豆瓣2.0
//
//  Created by dllo on 15/11/7.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "MovieCollectViewController.h"
#import "MovieDetailViewController.h"
#import "DataBaseSingleton.h"
#import "MovieList.h"

@interface MovieCollectViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain)UITableView *movieCTableView;
@property (nonatomic, retain)NSMutableArray *movieArray;

@end

@implementation MovieCollectViewController

- (void)dealloc
{
    [_movieCTableView release];
    [_movieArray release];
    [_nameTitle release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.movieCTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375, 667 - 60) style:UITableViewStylePlain];
    self.movieCTableView.delegate = self;
    self.movieCTableView.dataSource = self;
    [self.view addSubview:self.movieCTableView];
    [_movieCTableView release];
#pragma mark -- 注册
    [self.movieCTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
#warning - 不知道的小秘密:去掉多余的cell
    self.movieCTableView.tableFooterView = [UIView new];
    
    NSString *name = [NSUSER objectForKey:@"name"];
    self.movieArray = [DATABASE selectCollectUserName:name];
}


#pragma mark - 询问哪行需要编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
#pragma mark - 每行的编辑样式(删除/添加)
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}
#pragma mark - 实现编辑逻辑
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
#pragma mark -- 先删数据库
        NSString *name = [[NSUserDefaults standardUserDefaults] objectForKey:@"name"];
        [[DataBaseSingleton shareDataBase] deleteUserName:name Title:[self.movieArray objectAtIndex:indexPath.row]];
        NSLog(@"=======%@", name);
        NSLog(@"************%@", [self.movieArray objectAtIndex:indexPath.row]);
#pragma mark -- 再删cell
        [self.movieArray removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationTop];
    }
}
#pragma mark - 设置删除按钮文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"离开帅气的我吧!";
}

#pragma mark -- 区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

#pragma mark -- 行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.movieArray.count;
}
#pragma mark -- 设置cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdntifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdntifier];
    self.nameTitle = [self.movieArray objectAtIndex:indexPath.row];
    
    cell.textLabel.text = self.nameTitle;
    
    return cell;
}
#pragma mark -- 点击cell触发的方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    // 取消置灰
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSMutableArray *array = [NSMutableArray array];
    MovieDetailViewController *mDVC = [[MovieDetailViewController alloc] init];
    array = [DATABASE selectMovieName:[self.movieArray objectAtIndex:indexPath.row]];
    MovieList *movieL = [array lastObject];
    mDVC.movieL = movieL;
    mDVC.movieD.title = movieL.movieName;
    mDVC.navigationItem.title = movieL.movieName;
    
    [self.navigationController pushViewController:mDVC animated:YES];
   
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
